package com.hui.xiao.xiaohui.res;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.hui.xiao.xiaohui.R;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2016/5/30.
 */
public class BitmapTest extends Activity {
    String[] images = null;
    AssetManager assetManager = null;
    int currentImg = 0;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bitmap);
        image = (ImageView) findViewById(R.id.image);
        assetManager = getAssets();
        try {
            images = assetManager.list("");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Button next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentImg >= images.length) {
                    currentImg = 0;
                }
                while (!images[currentImg].endsWith(".png")
                        && !images[currentImg].endsWith(".jpg")
                        && !images[currentImg].endsWith(".gif")
                        ) {
                    currentImg++;
                    if (currentImg >= images.length) {
                        currentImg = 0;
                    }
                }
                InputStream assetFile = null;
                try {
                    assetFile = assetManager.open(images[currentImg++]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
                if (bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()) {
                    bitmapDrawable.getBitmap().recycle();
                }
                image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
            }
        });
    }
}
